package diploma.kisel.modelas.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.List;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.SpringLayout;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JButton;

import diploma.kisel.modelas.domain.DeviceType;
import diploma.kisel.modelas.domain.Project;
import diploma.kisel.modelas.gui.common.CurrentVar;
import diploma.kisel.modelas.service.ProjectServiceImpl;
import diploma.kisel.modelas.service.api.ProjectService;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * This window is designed for choosing current project.
 * Users can create many project and they can change them.
 * 
 * @author Nikolay Kisel
 *
 */
public class CurrentProjectWindow extends JFrame {

	private JPanel contentPane;
	private JTextField textFieldName;
	private JTextField textFieldShortName;
	private JTextField textFieldVersion;
	private JTextField textFieldDate;
	private JComboBox comboBox;

	private static CurrentProjectWindow frame;
	
	private ProjectService projectService = new ProjectServiceImpl();
	
//	private static Project currentProject;
	
	private static int index=0;

	/**
	 * Create the frame.
	 */
	public CurrentProjectWindow() {
		setTitle("Текущий проект");
//		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 580, 243);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		SpringLayout sl_contentPane = new SpringLayout();
		contentPane.setLayout(sl_contentPane);

		JLabel label = new JLabel("Выберете текущий проект:");
		sl_contentPane.putConstraint(SpringLayout.NORTH, label, 10,
				SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, label, 10,
				SpringLayout.WEST, contentPane);
		contentPane.add(label);

		comboBox = createComboBox();
		sl_contentPane.putConstraint(SpringLayout.NORTH, comboBox, -3,
				SpringLayout.NORTH, label);
		sl_contentPane.putConstraint(SpringLayout.WEST, comboBox, 50,
				SpringLayout.EAST, label);
		contentPane.add(comboBox);

		JLabel label_1 = new JLabel("Название:");
		sl_contentPane.putConstraint(SpringLayout.NORTH, label_1, 45,
				SpringLayout.SOUTH, label);
		sl_contentPane.putConstraint(SpringLayout.WEST, label_1, 10,
				SpringLayout.WEST, contentPane);
		contentPane.add(label_1);

		textFieldName = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, textFieldName, -3,
				SpringLayout.NORTH, label_1);
		sl_contentPane.putConstraint(SpringLayout.WEST, textFieldName, 39,
				SpringLayout.EAST, label_1);
		contentPane.add(textFieldName);
		textFieldName.setColumns(10);

		JLabel label_2 = new JLabel("Сокращенное название:");
		sl_contentPane.putConstraint(SpringLayout.WEST, label_2, 35,
				SpringLayout.EAST, textFieldName);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, label_2, 0,
				SpringLayout.SOUTH, label_1);
		contentPane.add(label_2);

		textFieldShortName = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, textFieldShortName,
				-3, SpringLayout.NORTH, label_1);
		sl_contentPane.putConstraint(SpringLayout.WEST, textFieldShortName, 28,
				SpringLayout.EAST, label_2);
		contentPane.add(textFieldShortName);
		textFieldShortName.setColumns(10);

		JLabel label_3 = new JLabel("Версия:");
		sl_contentPane.putConstraint(SpringLayout.NORTH, label_3, 36,
				SpringLayout.SOUTH, label_1);
		sl_contentPane.putConstraint(SpringLayout.WEST, label_3, 0,
				SpringLayout.WEST, label);
		contentPane.add(label_3);

		textFieldVersion = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, textFieldVersion, -3,
				SpringLayout.NORTH, label_3);
		sl_contentPane.putConstraint(SpringLayout.WEST, textFieldVersion, 0,
				SpringLayout.WEST, textFieldName);
		contentPane.add(textFieldVersion);
		textFieldVersion.setColumns(10);

		JLabel label_4 = new JLabel("Дата:");
		sl_contentPane.putConstraint(SpringLayout.NORTH, label_4, 0,
				SpringLayout.NORTH, label_3);
		sl_contentPane.putConstraint(SpringLayout.WEST, label_4, 0,
				SpringLayout.WEST, label_2);
		contentPane.add(label_4);

		textFieldDate = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, textFieldDate, -3,
				SpringLayout.NORTH, label_3);
		sl_contentPane.putConstraint(SpringLayout.EAST, textFieldDate, 0,
				SpringLayout.EAST, textFieldShortName);
		contentPane.add(textFieldDate);
		textFieldDate.setColumns(10);

		JButton button = new JButton("Отмена");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.setVisible(false);
			}
		});
		sl_contentPane.putConstraint(SpringLayout.SOUTH, button, 0,
				SpringLayout.SOUTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, button, -10,
				SpringLayout.EAST, contentPane);
		contentPane.add(button);

		JButton button_1 = new JButton("Выбрать");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Project currentProject = (Project) comboBox.getSelectedItem();
				index = comboBox.getSelectedIndex();
				CurrentVar.setCurrentProject(currentProject);
				frame.setVisible(false);
			}
		});
		sl_contentPane.putConstraint(SpringLayout.NORTH, button_1, 0,
				SpringLayout.NORTH, button);
		sl_contentPane.putConstraint(SpringLayout.EAST, button_1, -6,
				SpringLayout.WEST, button);
		contentPane.add(button_1);
	}
	
	public JComboBox createComboBox() {
		List<Project> projects = projectService.findByUser(CurrentVar.getCurrentUser());
		Vector<Object> items = new Vector<Object>();
		for (Project project: projects) {
			items.add(project);
		}
		comboBox = new JComboBox(items);
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				index = comboBox.getSelectedIndex();
				init();
			}
		});
//		comboBox.setSelectedItem(device.getDeviceType().getId());
		Project currentProject = CurrentVar.getCurrentProject();
		if (currentProject!=null)
			comboBox.setSelectedItem(currentProject);
		return comboBox;
	}
	
	public void init() {
		comboBox.setSelectedIndex(index);
		Project currProject = (Project) comboBox.getSelectedItem();
		textFieldName.setText(currProject.getName());
		textFieldShortName.setText(currProject.getShortName());
		textFieldVersion.setText(currProject.getVersion());
		textFieldDate.setText(currProject.getStartDate().toString("dd-MM-yyyy"));
		
		
	}

	public static CurrentProjectWindow getFrame() {
		return frame;
	}

	public static void setFrame(CurrentProjectWindow frame) {
		CurrentProjectWindow.frame = frame;
	}


}
